/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.mail.MessagingException;
import session.stateful.StakeholderSessionBeanStateful;

/**
 *
 * @author TrungHieu-Henry
 */
@ManagedBean(name = "sendEmailBean")
@RequestScoped
public class SendEmailBean {

    @EJB
    private StakeholderSessionBeanStateful stakeholderSessionBean;
    private String user;
    private String password;
    private String subject;
    private List<String> recipients;
    private List<String> usernames;
    private String message;
    private String from = user + "@gmail.com";
    private String standardmsg;

    public SendEmailBean() {
    }

    public void sendEmail() throws MessagingException {
        System.out.print("1");
        recipients = stakeholderSessionBean.getEmails();
        System.out.print("2");
        stakeholderSessionBean.setPwd(getPassword());
        System.out.print("3");
        stakeholderSessionBean.setUser(getUser());
        System.out.print("4");
        usernames = stakeholderSessionBean.getUsernames();   
        System.out.print("5");
        for (int i = 0; i < getRecipients().size(); i++) {
            System.out.print("for 1");
            standardmsg = "Hi "+ usernames.get(i).toString() +", \n";           
            System.out.print("for 2");
            stakeholderSessionBean.sendMail(recipients.get(i),getSubject(),standardmsg + getMessage(), getFrom());
            System.out.print("for 3");
        }
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public List<String> getRecipients() {
        return recipients;
    }

    public void setRecipients(List<String> recipients) {
        this.setRecipients(recipients);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public List<String> getUsernames() {
        return usernames;
    }

    public void setUsernames(List<String> usernames) {
        this.usernames = usernames;
    }
}
